<?php


namespace app\api\model;


use app\common\model\BaseModel;
use common;

class NoticeModel extends BaseModel
{
    protected $name = 'notice';
    protected $pk = 'id';


    function add($info)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'type' => 'int',
            'img' => 'string',
            'url' => 'string',
            'remark' => 'string',
            'start_time' => 'string',
            'expire_time' => 'string',
            'state' => 'int',
        ];

        $info = common::type_conver($info, $selectlist);

        self::create($info,true);
        return $info;
    }

    function update_info($id, $data)
    {
        $where = [
            'id' => $id,
        ];

        $selectlist = [
            'city_id' => 'string',
            'name' => 'string',
            'type' => 'int',
            'img' => 'string',
            'url' => 'string',
            'remark' => 'string',
            'start_time' => 'string',
            'expire_time' => 'string',
            'state' => 'int',
        ];

        $data = common::type_conver($data, $selectlist);
        $data['update_time'] = date('Y-m-d H:i:s');

        self::update($data,$where);
    }

    public function scopeCity($query,$city_id)
    {
        if (empty($city_id)){
            $query->whereNull('city_id');
        }
        else{
            $query->where('city_id',$city_id)->whereOr('city_id',null);
        }
    }

    function query_list($cd)
    {
        $beans[]=['start_time','<',date('Y-m-d H:i:s')];
        $beans[]=['expire_time','>',date('Y-m-d H:i:s')];

        if (isset($cd['type'])) {
            $beans[] = ['type','=',$cd['type']];
        }

        $city_id = null;
        if (isset($cd['city_id'])){
            $city_id = $cd['city_id'];
        }
        return $this->city($city_id)->where($beans)->select();
    }
}